<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.util.ArrayList"%>
<%@page import="model.SinhVienModel"%>
<%@page import="common.Constant" %>
<%@page import="dao.SinhVienDAO"%>
<%@page import="dao.DangKyDAO"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Thông Tin Sinh Viên</title>
<link href="../style/css.css" rel="stylesheet" type="text/css" />
<link href="../<%=request.getContextPath()%>/script/SinhVien_Script.js" language="javaScript" type="text/javascript" />
<%String tenSV=""; String diaChi=""; %>
<%int indexPage=1; %>
<c:if test="${ not empty param.tenSV }">
	<% tenSV=request.getParameter("tenSV"); %>
</c:if>
<c:if test="${ not empty param.diaChi }">
	<% diaChi=request.getParameter("diaChi"); %>
</c:if>
<c:if test="${ not empty param.index}">
	<% indexPage=Integer.parseInt(request.getParameter("index")); %>
</c:if>
</head>
<c:set var='sinhVienList' value='<%= SinhVienDAO.getAllSinhVienByTen(indexPage,Constant.NUM_RECORD_SinhVien,tenSV,diaChi)%>'></c:set>	
<c:set var='showNumPage' value='<%=SinhVienDAO.showNumPage(indexPage,SinhVienDAO.getTotalSinhVienByTen(tenSV,diaChi),"")%>'></c:set>
<body>
<div align="center">
	<!-- S HEAD CONTENT -->
	<jsp:include page="../header.jsp" />
<!-- E HEAD CONTENT -->
		<c:if test="${ not empty param.errUpdate}">
			<c:if test="${param.errUpdate eq 'false'}">
					<font color='blue' style='font-weight:bold'>Cập nhật thành công</font>
					<br/>
					<br/>
			</c:if>
			<c:if test="${param.errUpdate eq 'true'}">
					<font color='red' style='font-weight:bold'>Cập nhật thất bại</font>
					<br/>
					<br/>
			</c:if>
		</c:if>
		<c:if test="${ not empty param.errInsert}">
			<c:if test="${param.errInsert eq 'false'}">
					<font color='blue' style='font-weight:bold'>Tạo mới thành công</font>
					<br/>
					<br/>
			</c:if>
			<c:if test="${param.errInsert eq 'true'}">
					<font color='red' style='font-weight:bold'>Tạo mới thất bại</font>
					<br/>
					<br/>
			</c:if>
		</c:if>			
			
		<form name="frmSinhVien" id="frmSinhVien" method="post" action="<%=request.getContextPath()%>/thanhVienController" >
			<input type="hidden" name="actionType" id="actionType" value="sinhVien" />
			<div>
				<table>
					<tr style="background-color: transparent;">
						<td colspan="9">
							<h3>Danh Sách Sinh Viên</h3>
						</td>
					</tr>
					<tr style="background-color: transparent;">
						<td colspan="9">
							<c:if test="${not empty param.xoa}">
								<b class="msg">Xoá sinh viên thành công !!!</b>
							</c:if>
						</td>
					</tr>
				</table>
					
				<table border="1" >	
					<tr>
						<td colspan="12" valign="middle">
							Tên sinh viên <input type="text" name="txtTimTenSinhVien" id="txtTimTenSinhVien"/> 
							Địa chỉ <input type="text" name="txtTimDiaChi" id="txtTimDiaChi"/>
								<a href = "javascript: click_btnTimTenSV()">
									<img src="<%=request.getContextPath()%>/images/timkiem.png" border = "0" />
								</a>
								&nbsp;&nbsp;
								<a href = "javascript: deleteRow()"><img src="<%=request.getContextPath()%>/images/xoa.png" border = "0" /></a>
								<a href = "danhsachsinhvien.jsp?stt=insert&tenSV=<%=tenSV %>&index=<%=indexPage %>">
									<img src="<%=request.getContextPath()%>/images/them.png" border = "0" />
								</a>
						</td>
					</tr>				
					<tr>
						<td colspan="12" style="text-align:right">	
							Trang 
							<c:forEach var="numPage" items="${showNumPage}">
								${numPage} 
							</c:forEach>									
						
						</td>
						
					</tr>
					<tr>
						<th bgcolor = "#186fb2">
								<div class = "div_textWhite">
									<input type="checkbox" name="chkAll" id="chkAll" onclick="checkAll();"></input>
								</div>
						</th>
						<th style='background-color: #186fb2;color:white'>STT</th>
						<th style='background-color: #186fb2;color:white'>Mã sinh viên</th>
						<th style='background-color: #186fb2;color:white'>Tên sinh viên</th>
						<th style='background-color: #186fb2;color:white'>Ngày sinh</th>
						<th style='background-color: #186fb2;color:white'>Giới tình</th>
						<th style='background-color: #186fb2;color:white'>Địa chỉ</th>
						<th style='background-color: #186fb2;color:white' colspan="2"></th>
					</tr>
					<c:if test="${param.stt eq 'insert'}">
						<tr>
							<td colspan='7'>
								<br/>
								<br/>
								<font style='font-weight:bold'>Thêm mới sinh viên</font>
								<br/>
								<br/>
								<table class="tableSinhVien">
										<tr><td colspan="12"><font color='red' id="alertValidate"></font></td></tr>
										<tr>
											<td width="30%" style='font-weight:bold'>Mã sinh viên</td>
											<td width="70%">
												<input type='text' name="txtMaSinhVien" id="txtMaSinhVien"  />
												<font color='red' id='alertMaSinhVien'></font>
											</td>
										</tr>
										<tr>
											<td style='font-weight:bold'>Tên sinh viên</td>
											<td width="70%">
												<input type='text' name="txtTenSinhVien" id="txtTenSinhVien" />
												<font color='red' id='alertTenSinhVien'></font>
											</td>
										</tr>
										<tr>
											<td style='font-weight:bold'>Ngày sinh</td>
											<td>
												<select name="selNgay" id="selNgay">
													<c:forEach var="Ngay" begin="01" end="31">
														<option value="${Ngay}">${Ngay}</option>
													</c:forEach>
												</select>
												<font color='red' id='alertNgay'></font> /
												
												<select name="selThang" id="selThang">
													<c:forEach var="Thang" begin="01" end="12">
														<option value="${Thang}">${Thang}</option>
													</c:forEach>
												</select>/
												
												<select name="selNam" id="selNam">
													<c:forEach var="Nam" begin="1900" end="2005">
														<option value="${Nam}">${Nam}</option>
													</c:forEach>
												</select>												
											</td>
										</tr>
										<tr>
											<td style='font-weight:bold'>Giới tính</td>
											<td>
												<input type='text' name="txtGioiTinh" id="txtGioiTinh"/>
												<font color='red' id='alertGioiTinh'></font></td>
										</tr>
										<tr>
											<td style='font-weight:bold'>Địa chỉ</td>
											<td>
												<input type='text' name="txtDiaChi" id="txtDiaChi"/>
												<font color='red' id='alertDiaChi'></font></td>
										</tr>
										
										<tr>	
											<td colspan="12" style="text-align:center">
											<a href = "javascript: click_btnThemMoi()">	
													<img src="<%=request.getContextPath()%>/images/chapnhan.png" border = "0" />
												</a>
												<a href = "javascript: click_btnThoat('<%=request.getParameter("index")%>')">	
													<img src="<%=request.getContextPath()%>/images/thoat.png" border = "0" />
												</a>
											</td>
										</tr>
									</table>
								
							</td>
						</tr>
					
					</c:if>
						<%int c = 1; 
						ArrayList<SinhVienModel> sinhVienList;
						if(request.getSession().getAttribute("sinhVienModelList") != null)
							sinhVienList = (ArrayList<SinhVienModel>)request.getSession().getAttribute("sinhVienModelList");
						else
						sinhVienList = SinhVienDAO.getAllSinhVien();
					%>
					<c:forEach var="objSinhVien" items="${sinhVienList}">
							<c:choose>
								<c:when test="${param.stt eq 'view' and objSinhVien.maSV eq param.MaSV}">
									<tr><td  colspan="12">
										<table class="tableSinhVien">
											<tr>
												<td width="30%" style='font-weight:bold'>Mã sinh viên</td>
												<td width="70%">${objSinhVien.maSV }</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Tên sinh viên</td>
												<td>${objSinhVien.tenSV }</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Ngày sinh</td>
												<td>${objSinhVien.ngaySinh }</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Giới tính</td>
												<td>${objSinhVien.gioiTinh }</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Địa chỉ</td>
												<td>${objSinhVien.diaChi }</td>
											</tr>
										</table>		
									</td></tr>
								</c:when>
								<c:when test="${param.stt eq 'edit' and objSinhVien.maSV eq param.MaSV}">
									<input type='hidden' value='${objSinhVien.maSV}' name='txtMaSinhVien'></input>
									<tr><td colspan="9">
										<table class="tableSinhVien">
											<tr><td colspan="7"><font color='red' id="alertValidate"></font></td></tr>
											
											<tr>
												<td width="30%" style='font-weight:bold'>Mã sinh viên</td>
												<td width="70%"> 
													<input type='text' value="${objSinhVien.maSV }" readonly="readonly"/></td>
											</tr>
											<tr>
												<td width="30%" style='font-weight:bold'>Tên sinh viên</td>
												<td width="70%"> 
													<input type='text' name="txtTenSinhVien" id="txtTenSinhVien" value="${objSinhVien.tenSV }"/>
													<font color='red' id='alertTenSinhVien'></font></td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Ngày sinh</td>
												<td>
													<input type='text' value='${objSinhVien.ngaySinh}' name="txtNgaySinh" id="txtNgaySinh"/>
													<font color='red' id='alertNgaySinh'></font>(yyyy/mm/dd)</td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Giới tính</td>
												<td>
													<input type='text' value='${objSinhVien.gioiTinh}' name="txtGioiTinh" id="txtGioiTinh"/>
													<font color='red' id='alertGioiTinh'></font></td>
											</tr>
											<tr>
												<td style='font-weight:bold'>Địa chỉ</td>
												<td>
													<input  type='text' value='${objSinhVien.diaChi}' name="txtDiaChi" id="txtDiaChi"/>
													<font color='red' id='alertDiaChi'></font></td>
											</tr>											
											
											<tr>
												<td colspan="9" style="text-align:center">
												
													<a href = "javascript: click_btnCapNhat()">
														<img src="<%=request.getContextPath()%>/images/chapnhan.png" border = "0" />
													</a>
													<a href = "javascript: click_btnThoat('<%=request.getParameter("index")%>')">
														<img src="<%=request.getContextPath()%>/images/thoat.png" border = "0" />
													</a>											
												</td>												
											</tr>
										</table>
									
									</td></tr>
								</c:when>
								<c:otherwise>
									<c:set var="sinhVienList" value="<%= sinhVienList %>" scope = "session"></c:set>
									<tr>
											<td><input type="checkbox" name="chk<%=c %>" id="chk<%=c %>" onclick="checkRow();"/></td>
											<td><%=c %></td>
											<td>${objSinhVien.maSV }</td>
											<td><a href = "chitietsinhvien.jsp?maSinhVien=${objSinhVien.maSV }">${objSinhVien.tenSV }</a></td>
											<td>${objSinhVien.ngaySinh }</td>
											<td>${objSinhVien.gioiTinh }</td>
											<td>${objSinhVien.diaChi }</td>
											<td><a href='danhsachsinhvien.jsp?stt=edit&tenSV=<%=tenSV %>&index=<%=indexPage %>&MaSV=${objSinhVien.maSV}'>Edit</a></td>
											<td><a href='danhsachsinhvien.jsp?stt=view&tenSV=<%=tenSV %>&index=<%=indexPage %>&MaSV=${objSinhVien.maSV}'>View</a></td>
									</tr>
								</c:otherwise>
						</c:choose>
						<%c++; %>
					</c:forEach>
					<input type="hidden" name="xoaSV" id="xoaSV"/>
					
			<script language="JavaScript" type="text/javascript">
				function submit()
				{
					document.getElementById("frmSinhVien").submit();
				}
			
				function checkAll()
				{		
					if (document.getElementById("chkAll").checked == true) {
						<%
							for(int i = 1; i< c; i++) {
								out.print("document.getElementById('chk" + i + "').checked = true;\n");
							}
						%>	
					}else {
						<%
							for(int i = 1; i< c; i++) {
								out.print("document.getElementById('chk" + i + "').checked = false;\n");
							}
						%>
					}
				}
				
				function checkRow(){
					if(document.getElementById('chkAll').checked == true)
						document.getElementById('chkAll').checked = false;
				}
				
				function deleteRow()
				{
					var check = false;
					<%
						for(int i=1;i<c;i++)
						{
							out.print("if(document.getElementById('chk" + i + "').checked == true)");
							out.print("check = true;");
						}
					%>
					if(check == true)
					{
						if(confirm("Bạn có chắc muốn xoá không???"))
						{
							document.getElementById('xoaSV').value = 'xoa';
							document.forms["frmSinhVien"].submit();
						}
					}
					else
						alert("Bạn chưa chọn checkbox nào!!!")
				}
				
				function rightSpace(str)
				{
					var length=str.length-1;
					while(length>=0&&str.charAt(length)==' ')
					{
						length--;
					}
					return str.substr(0,length+1);
				}
				
				function validateNumber(str)
				{
					validate="0123456789";
					for(i=0;i<str.length;i++)
					{
						for(j=0;j<validate.length;j++)
							if(str.charAt(i)==validate.charAt(j))
								j=validate.length;
						if(j!=validate.length+1)
							return false;
					}
					return true;
				}
				
				function validateForm_Insert()
				{
					var maSinhVien=document.getElementById("txtMaSinhVien").value;
					var tenSinhVien=document.getElementById("txtTenSinhVien").value;
					var ngay=document.getElementById("selNgay").value;
					var thang=document.getElementById("selThang").value;
					var nam=document.getElementById("selNam").value;
					var gioiTinh=document.getElementById("txtGioiTinh").value;
					var diaChi=document.getElementById("txtDiaChi").value;			
					var stt=true;					
					
					if(rightSpace(maSinhVien)!="")
						document.getElementById("alertMaSinhVien").innerHTML="";
					else{
						document.getElementById("alertMaSinhVien").innerHTML="*";
						stt=false;
					}
									
					if(rightSpace(tenSinhVien)!="")
						document.getElementById("alertTenSinhVien").innerHTML="";
					else{
						document.getElementById("alertTenSinhVien").innerHTML="*";
						stt=false;
					}
					
					if(rightSpace(gioiTinh)!=""){
						document.getElementById("alertGioiTinh").innerHTML="";			
					}
					else{
						document.getElementById("alertGioiTinh").innerHTML="*";
						stt=false;
					}
					
					if(rightSpace(diaChi)!="")
						document.getElementById("alertDiaChi").innerHTML="";
					else{
						document.getElementById("alertDiaChi").innerHTML="*";
						stt=false;
					}
					
					if(stt==false)
						document.getElementById("alertValidate").innerHTML="Bạn phải nhập vào nơi có dấu *";
					else{
						//nam nhuan o day//
					
						//chua co test//
						
						document.getElementById("alertValidate").innerHTML="";
						if(!validateNumber(ngay)){
							document.getElementById("alertNgay").innerHTML="*";
							stt=false;
						}
						else if(!validateNumber(thang)){
							document.getElementById("alertThang").innerHTML="*";
							stt=false;
						}
						else if(thang<1 || thang>12){
							document.getElementById("alertThang").innerHTML="*";
							stt=false;
						}
						else if(ngay<1 || ngay>31){
							document.getElementById("alertNgay").innerHTML="*";
							stt=false;
						}
						else if(!validateNumber(nam)){
							document.getElementById("alertNam").innerHTML="*";
							stt=false;
						}
						else if(nam<1900 || nam>2005){
							document.getElementById("alertNam").innerHTML="*";
							stt=false;
						}
						else
							document.getElementById("alertNgay").innerHTML="";
					}	
					return stt;	
				}
				
				function click_btnTimTenSV()
				{					
					location.href="danhsachsinhvien.jsp?tenSV="+document.getElementById("txtTimTenSinhVien").value+"&diaChi="+document.getElementById("txtTimDiaChi").value;					
				}
				
				function validateForm()
				{
					var tenSinhVien=document.getElementById("txtTenSinhVien").value;
					var ngaySinh=document.getElementById("txtNgaySinh").value;
					var gioiTinh=document.getElementById("txtGioiTinh").value;
					var diaChi=document.getElementById("txtDiaChi").value;			
					var stt=true;					
					
					if(rightSpace(tenSinhVien)!="")
						document.getElementById("alertTenSinhVien").innerHTML="";
					else{
						document.getElementById("alertTenSinhVien").innerHTML="*";
						stt=false;
					}
					
					if(rightSpace(ngaySinh)!=""){
						document.getElementById("alertNgaySinh").innerHTML="";			
					}
					else{
						document.getElementById("alertNgaySinh").innerHTML="*";
						stt=false;
					}
					
					if(rightSpace(gioiTinh)!=""){
						document.getElementById("alertGioiTinh").innerHTML="";			
					}
					else{
						document.getElementById("alertGioiTinh").innerHTML="*";
						stt=false;
					}
					
					if(rightSpace(diaChi)!="")
						document.getElementById("alertDiaChi").innerHTML="";
					else{
						document.getElementById("alertDiaChi").innerHTML="*";
						stt=false;
					}
					
					if(stt==false)
						document.getElementById("alertValidate").innerHTML="Bạn phải nhập vào nơi có dấu *";
					else{
						document.getElementById("alertValidate").innerHTML="";
					}	
					return stt;	
				}
				
				function click_btnCapNhat()
				{					
					if(validateForm())
					{
						document.getElementById("actionType").value="capnhatsinhvien";
						document.forms['frmSinhVien'].submit();
					}
				}

				function click_btnThemMoi()
				{
					if(validateForm_Insert())
					{
						document.getElementById("actionType").value="themmoisinhvien";
						document.forms['frmSinhVien'].submit();
					}	
				}
		
				function click_btnThoat(indexPage)
				{
					location.href="danhsachsinhvien.jsp?tenSV="+document.getElementById("txtTimTenSinhVien").value+"&index="+indexPage;		
				}		
				
			</script>					
					<tr>
						<td colspan="12" style="text-align:right">
						Trang 
							<c:forEach var="numPage" items="${showNumPage}">
								${numPage} 
							</c:forEach>									
						</td>
					</tr>
				</table>
				<br/>
					
			</div>
			<!-- Lay duong cac properties cua  link  -->
				<% String proLink=""; %>
				<c:if test="${not empty param.tenSV}"><% proLink="&tenSV="+request.getParameter("tenSV"); %> </c:if>
				<c:if test="${not empty param.index}"><% proLink=proLink+"&index="+request.getParameter("index"); %> </c:if>
				
				<input type='hidden' value="<%=proLink%>" name="txtProLink" id="txtProLink"/>
			<!-- End -->
		</form>

	<div class="div_footer"><br/>
				Copyright©2011. All right reserved.Theme design by HungVuong Group.
			</div>
</div>
</body>
</html>
